package org.jiamandan.com.strategy.impl.tram.listener;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jiamandan.com.constant.BizConstant;
import org.jiamandan.com.constant.JSYConstant;
import org.jiamandan.com.dto.JsyBasicDto;
import org.jiamandan.com.strategy.impl.tram.IJSYDZBasicListener;
import org.jiamandan.com.utils.JsyObjectUtil;
import org.jiamandan.com.utils.RedisUtil;
import org.springframework.stereotype.Service;


@Service(value = JSYConstant.Service.CMD_JSY_T00301_SERVICE)
@AllArgsConstructor
@Slf4j
public class JSYPunchTheCardListener implements IJSYDZBasicListener {

    private RedisUtil redisUtil;


    @Override
    public void doExecute(String payload, ChannelHandlerContext client) throws Exception {
        log.info("嘉满电-JSY刷卡上报(CMD = T00301)：" + payload);
        JsyBasicDto jsyBasicDto = JsyObjectUtil.parse(payload);
        String[] items = StringUtils.split(jsyBasicDto.getData(), BizConstant.separator);
        log.info("嘉满电-JSY刷卡上报:设备号:"+jsyBasicDto.getDeviceNo()+"端口号"+items[0]+"卡号:"+items[1]);
        String pileNo = jsyBasicDto.getDeviceNo();
        String gunNo = items[0];
        String cardNo = items[1];
        //01未注册（语音“未注册卡”)   未添加卡号或未绑定微信用户
        reply(client,"/0001:2",jsyBasicDto.getDeviceNo()+":"+gunNo+":充电桩不存在");
        reply(client,"/0004:4/"+gunNo,jsyBasicDto.getDeviceNo()+":"+gunNo+":充电枪桩不存在");
        reply(client,"/0001:2",jsyBasicDto.getDeviceNo()+":"+gunNo+":卡不存在1");
        reply(client,"/0001:2",jsyBasicDto.getDeviceNo()+":"+gunNo+":卡不存在2");
        reply(client,"/0004:4/"+gunNo,jsyBasicDto.getDeviceNo()+":"+gunNo+":离线");
        reply(client,"/0001:3",jsyBasicDto.getDeviceNo()+":"+gunNo+":充电中");
        reply(client,"/0004:4/"+gunNo,jsyBasicDto.getDeviceNo()+":"+gunNo+":未空闲");
        reply(client,"/0004:4/"+gunNo,jsyBasicDto.getDeviceNo()+":"+gunNo+":计费模型未设置");
        reply(client,"/0001:2",jsyBasicDto.getDeviceNo()+":"+gunNo+":卡未绑定");
        reply(client,"/0001:2",jsyBasicDto.getDeviceNo()+":"+gunNo+":卡绑定用户不存在");
        reply(client,"/0001:2",jsyBasicDto.getDeviceNo()+":"+gunNo+":用户已被停用");
        reply(client,"/0001:2",jsyBasicDto.getDeviceNo()+":"+gunNo+":用户余额不足1元");
        reply(client,"/0001:3",jsyBasicDto.getDeviceNo()+":"+gunNo+":站点为空");
        reply(client,"/0001:2",jsyBasicDto.getDeviceNo()+":"+gunNo+":无效卡，不是同一个代理商下");
        reply(client,"/0001:3",jsyBasicDto.getDeviceNo()+":"+gunNo+":站点状态不是正常使用");
        log.info("创建订单");
    }


    private  void reply(ChannelHandlerContext client,String directive,String reason){
        log.info("嘉满电-JSY刷卡应答(CMD = S00301)结果:"+reason);
        StringBuilder builder = new StringBuilder();
        builder.append("_"+ JSYConstant.Cmd.S00301).append(directive).append("\r\n");
        log.info("嘉满电-JSY刷卡应答(CMD = S00301):" + builder.toString());
        ByteBuf buf = Unpooled.buffer(builder.toString().getBytes().length);
        buf.writeBytes(builder.toString().getBytes());
        ChannelFuture future = client.writeAndFlush(buf);
    }

}
